/*
 * 描述 ; 用于抓取页面模板进行加载到导航部位
 * 参数 : normalTemp : script中普通模板 , roleTemp 角色情况下的模板 , navData导航数据
 * 作者 : liubibo
 * */
function navHoverTemp(normalTemp,roleTemp,navData) {
    var thisFun = arguments.callee;
    /**
     * 描述 : 用于分类列表的js模板加载
     * 参数 : html为模板html，target是将模板插入位置，data为模板内部参数填充, isPrepend为true时候，模板追加，不然直接覆盖
     * 作者 : liubibo
     */
    thisFun.normalTL = function (html, target, data, isPrepend) {
        var tlCategory = $(html).html();
        var renderer = Handlebars.compile(tlCategory);
        Handlebars.registerHelper('listenRole', function (listenRole, options) {
            if (listenRole) {
                return 'jsStoreRole';
            }
        });
        var result = renderer(data);
        if (!isPrepend) {
            $(target).html(result);
        } else {
            $(target).prepend(result);
        }
    };

    /**
     * 描述 : 用于根据传递的角色，找到对应的数据，进行填充模板
     * 参数 : html为模板html，target是将模板插入位置，data为模板内部参数填充, isPrepend为true时候，模板追加，不然直接覆盖，role为寻找对应的角色
     * 作者 : liubibo
     */
    thisFun.roleList = function (html, target, data, isPrepend, role) {
        var renderer = Handlebars.compile(html);
        Handlebars.registerHelper('roleList', function (lists, options) {
            var roleLists = "";
            var ret = "";
            $.each(lists, function (ind, item) {
                if (item.subList) {
                    $.each(item.subList, function (ind2, item2) {
                        if ($.trim(role) == $.trim(ind2)) {
                            roleLists = item2.lowestList;
                        }
                    });
                }
            });
            if (!!roleLists) {
                $.each(roleLists, function (i, t) {
                    ret += options.fn(t);
                });
                return ret;
            } else {
                console.warn('数据结构没有匹配上');
            }
        });
        Handlebars.registerHelper('roleName', function (lists, options) {                                               //用与展示页面导航中，当前所属类
            var roleName = "";
            $.each(lists, function (ind, item) {
                if (item.subList) {
                    $.each(item.subList, function (ind2, item2) {
                        if ($.trim(role) == $.trim(ind2)) {
                            roleName = ind2;
                        }
                    });
                }
            });
            return roleName;
        });
        Handlebars.registerHelper('listenRole', function (listenRole, options) {
            if (listenRole) {
                return 'jsStoreRole';
            }
        });
        var result = renderer(navData);
        $('.nav-hover').html(result);
    };

    /**
     * 描述 : 用于存储角色，如果已经存在，直接刷新时间数据。否则直接添加
     * 参数 : key为本地存储键值，val为需要存储数据，changeState是否要改变存储时间
     * 作者 : liubibo
     */
    thisFun.storeRole = function (key, val, changeState) {
        var loc = !!window.localStorage ? window.localStorage : "";
        if (!!window.localStorage && !!key) {
            var locState = loc.getItem(key);
            if (!!locState) {
                if (thisFun.checkOutDate(locState)) {                                                                   //存在角色
                    if (!!changeState) {
                        loc.setItem(key, val + ':' + (+new Date));
                        return false;
                    }
                    return true;
                } else {
                    if (!!changeState) {
                        loc.setItem(key, val + ':' + (+new Date));
                        return true;
                    }
                    return false;
                }
            } else {
                if (!!changeState) {                                                                                    //没有存储数据的时候，因为点击之后，存储关闭状态
                    loc.setItem(key, val + ':' + (+new Date));
                    return false;
                }
                return true;
            }
        } else {                                                                                                        //不支持，需要写兼容，保存cookie

        }
    };

    /**
     * 作用 : 移除本地存储
     * 作者 : liubibo
     */
    thisFun.removeRole = function (key) {
        var loc = !!window.localStorage ? window.localStorage.getItem(key) : "";
        if (loc) {
            window.localStorage.removeItem(key);
        }
    };

    /**
     * 作用 : 用于检测是否过期3天，如果没有过期， ...重新填入时间
     * 作者 : liubibo
     */
    thisFun.checkOutDate = function (val) {
        var timeReg = /^(.+?)\:(.+?)$/;
        var result = val.match(timeReg);
        var time = !!result && !!result ? result[2] : '';
        if (!!time && ( (+new Date) - time > 3600 * 24 * 3 )) {
            return true;
        } else {
            return false;
        }
    };

    /**
     * 作用 : 用于获取本地存储中的角色
     * 例子 : "role-state" :"EBAY客服中心:140021231321"; 数字为时间戳
     * 作者 : liubibo
     */
    thisFun.getRole = function (key) {
        var loc = !!window.localStorage ? window.localStorage.getItem(key) : "";
        if (loc) {
            var reg = /^(.+?)\:(.+?)$/;
            var result = loc.match(reg);
            var name = !!result ? result[1] : '';
            var time = !!result ? result[2] : '';
            return {name: name, time: time};
        } else {
            //兼容获取cookie
            return false;
        }
    };

    var role = thisFun.getRole('role-state');
    if (role) {                                                                                                         //获取本地存储角色，有则加载角色模板，没有则加载普通模板
        var html = $(roleTemp).html();
        thisFun.roleList(html, '.nav-hover', navData, false, role.name);
        $('.nav-hover').on('click', '.jsStoreHome a', function (e) {                                                    //点击订单执行中心，删除role-state
            e.preventDefault();
            var $this = $(this);
            var $storeRole = $this.closest('.jsStoreRole');
            if( $storeRole.length == 0) {
                thisFun.removeRole('role-state');
                if ($this.attr('href')) {
                    location.href = $(this).attr('href');
                }
            }else {
                val = $storeRole.data('value');
                //$storeRole.data('value')
                if (val) {
                    thisFun.storeRole('role-state', val, true);
                }
            }
        });
    } else {
        thisFun.normalTL(normalTemp, '.nav-hover', navData, false);
    }
    $('.nav-hover').on('click', '.jsStoreRole a', function (e) {                                                        //点击客服中心，删除role-state
        e.preventDefault();
        var $this = $(this);
        var val;
        var $parentLi = $this.closest('li');
        if( !(val = $parentLi.data('value')) ) {
            val = $parentLi.closest('.jsStoreRole').data('value');
        }
        if (val) {
            thisFun.storeRole('role-state', val, true);
        }
        if ($this.attr('href')) {
            location.href = $this.attr('href');
        }
    }).on('click','.not-url',function(e){                                                                               //阻止任何空连接跳转。
        e.preventDefault();
    });

    /**
     * 作用 : 根据访问目录改变页面头标题
     * 作者 : Wade
     */
    if (!!window.location.search) {
        var $title = $('head title');
        var title = '';
        $.each(navData, function (i, v) {
            if (!!v.href) {
                if (v.href.indexOf(window.location.search) >= 0) {
                    title = v.list + '-' + $title.html();
                }
            } else {
                $.each(v.subList, function (ii, vv) {
                    if (!vv.lowestList) {
                        if (vv.href.indexOf(window.location.search) >= 0) {
                            title = ii + '-' + v.list + '-' + $title.html();
                        }
                    } else {
                        $.each(vv.lowestList, function (iii, vvv) {
                            if (vvv.href.indexOf(window.location.search) >= 0) {
                                title = vvv.list + '-' + ii + '-' + $title.html();
                            }
                        });
                    }
                });
            }
        });
        !!title && $title.html(title);
    }
}
(function() {
    var navData = [                                                                                                     //顶部导航数组
        {
            list: "产品管理",
            // href: ROOT_URL + "?c=ctrl_ali_aliProduct&a=index",
            subList: {
                // "获取产品信息": {
                //     href: ROOT_URL + "?c=ctrl_ali_product&a=index"
                // },
                // "拼多多类目": {
                //     href: ROOT_URL + "?c=ctrl_pdd_pddCat&a=index"
                // },
                "整理店小秘刊登数据": {
                    href: ROOT_URL + "?c=ctrl_wish_product&a=index"
                },
                "采购链接查询": {
                    href: ROOT_URL + "?c=ctrl_wish_purchaseUrl&a=index"
                },
                "Wish产品统计": {
                    href: ROOT_URL + "?c=ctrl_wish_wishProduct&a=index",
                },
                "关键词查询工具": {
                    href: ROOT_URL + "?c=ctrl_wish_wishProduct&a=lookKeyword",
                },
                "wish产品数据下载": {
                    href: ROOT_URL + "?c=ctrl_wish_product&a=wishProductDownload",
                },
                "PB广告统计": {
                    href: ROOT_URL + "?c=ctrl_wish_pb&a=index",
                },
            }
        },
        {
            list: "订单管理",
            // href: ROOT_URL + "?c=ctrl_wish_dxmOrder&a=index",
            subList: {
                // "物流公司列表": {
                //     href: ROOT_URL + "?c=ctrl_pdd_pddLogistics&a=index"
                // },
                "店小秘订单": {
                    href: ROOT_URL + "?c=ctrl_wish_dxmOrder&a=index"
                },
                "扫描发货": {
                    href: ROOT_URL + "?c=ctrl_wish_scanShip&a=index"
                },
            }
        },
        {
            list: "报表",
            subList: {
                "少库存产品": {
                    href: ROOT_URL + "?c=ctrl_wish_report&a=getProductStock"
                },
                "待审核订单": {
                    href: ROOT_URL + "?c=ctrl_wish_report&a=getPendingOrder"
                },
                "订单数量": {
                    href: ROOT_URL + "?c=ctrl_wish_report&a=orderQty"
                },
                "店铺数据图表": {
                    href: ROOT_URL + "?c=ctrl_wish_chart&a=index"
                },
                "统计店铺余额": {
                    href: ROOT_URL + "?c=ctrl_wish_report&a=balanceWish"
                },
                "Wish账单明细": {
                    href: ROOT_URL + "?c=ctrl_wish_wishBalance&a=index"
                },
                "各个国家订单数量": {
                    href: ROOT_URL + "?c=ctrl_wish_report&a=countryQty"
                },
            }
        },
        {
            list: "系统管理",
            subList: {
                // "系统参数配置": {
                //     href: ROOT_URL + "?c=ctrl_config&a=index"
                // },
                // "消息队列管理": {
                //     href: ROOT_URL + "?c=ctrl_mqManage&a=index"
                // },
                "账号授权管理": {
                    href: ROOT_URL + "?c=ctrl_vsAccount&a=index"
                },
                "店小秘Cookie": {
                    href: ROOT_URL + "?c=ctrl_vsAccountCookie&a=index"
                },
                "Wish买家账号": {
                    href: ROOT_URL + "?c=ctrl_wishBuyAccount&a=index"
                },
                "Wish店铺Cookie": {
                    href: ROOT_URL + "?c=ctrl_wishSellerAccount&a=index"
                },
                // "代码生成器": {
                //     href: ROOT_URL + "?c=ctrl_codeGenerator&a=index"
                // },
            }
        },
        {
            list: "其它系统跳转",
            subList: {
                "店小秘": {
                    href: "https://www.dianxiaomi.com/home.htm"
                },
                "wish后台": {
                    href: "https://china-merchant.wish.com/"
                },
                "wish邮": {
                    href: "https://www.wishpost.cn/"
                },
            }
        },
        // {
        //     list: "订单管理",
        //     href: ROOT_URL + "?c=ctrl_ali_product&a=index",
        //     subList: {
        //         "拼多多": {
        //             listenRole: true,
        //             href: ROOT_URL + "?c=ctrl_pdd_pddOrder&a=index",
        //             lowestList: [
        //                 {
        //                     list: "订单列表",
        //                     href: ROOT_URL + "?c=ctrl_pdd_pddOrder&a=index"
        //                 },
        //             ]
        //         },
        //     }
        // },

    ];
    navHoverTemp('#normalTemp','#roleTemp',navData);
})();

